import Vue from 'vue'
import VueRouter from 'vue-router'

if (!window.VueRouter) Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    meta: {title: '数据可视化'},
    component: () => import('../views/index')
  }
]

const router = new VueRouter({
  routes,
  mode: 'hash',
  // base: '/',
  scrollBehavior(to, from, savedPosition) {
    return {x: 0, y: 0}
  }
})

router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  const targetPath = router.history.pending.fullPath;
  if (isChunkLoadFailed) {
    router.replace(targetPath);
  }
})

export default router
